package com.sc.test;

import android.animation.ValueAnimator;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieDrawable;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Shen Chao on 2018-03-17-017 下午 15:43.
 */

public class LottieActivity extends AppCompatActivity {
    @BindView(R.id.lav_one)
    LottieAnimationView lavOne;
    @BindView(R.id.lav_two)
    LottieAnimationView lavTwo;
    @BindView(R.id.lav_three)
    LottieAnimationView lavThree;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lottie);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.bt_lottie1, R.id.bt_lottie2, R.id.bt_lottie3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_lottie1:
                //动画执行再倒着执行，3秒执行完毕
                ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 1f, 0f)
                        .setDuration(5000);
                //重复3次
                valueAnimator.setRepeatCount(3);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        lavOne.setProgress((Float) animation.getAnimatedValue());
                    }
                });
                valueAnimator.start();
                break;
            case R.id.bt_lottie2:
                //加载动画
                lavTwo.setAnimation("2.json");
                //循环播放
//                lavTwo.loop(true);    已废弃
                //循环多少次LottieDrawable.INFINITE无数次
                lavTwo.setRepeatCount(LottieDrawable.INFINITE);
                //播放动画
                lavTwo.playAnimation();
                break;
            case R.id.bt_lottie3:
//                Toast.makeText(this, "现在动画路径加载不到，以后处理", Toast.LENGTH_LONG).show();

//                //加载动画
//                lavThree.setAnimation("cat/data.json");
//                lavThree.setImageAssetsFolder("cat/images");
//                //循环播放
////                lavTwo.loop(true);    已废弃
//                //循环多少次LottieDrawable.INFINITE无数次
//                lavThree.setRepeatCount(LottieDrawable.INFINITE);
//                //播放动画
//                lavThree.playAnimation();


                //网络加载动画
//                OkHttpClient httpClient = new OkHttpClient();
//                final Request request = new Request.Builder()
//                        .url("https://lottiefiles.com/download/public/3907")
//                        .build();
//                httpClient.newCall(request).enqueue(new Callback() {
//                    @Override
//                    public void onFailure(Call call, IOException e) {
//
//                    }
//
//                    @Override
//                    public void onResponse(Call call, Response response) throws IOException {
//                        try {
//                            String json = response.body().string();
//                            Cancellable cancellable = LottieComposition.Factory.fromJsonString(json, new OnCompositionLoadedListener() {
//                                @Override
//                                public void onCompositionLoaded(@Nullable LottieComposition composition) {
//                                    lavThree.setComposition(composition);
//                                    lavThree.setRepeatCount(LottieDrawable.INFINITE);
//                                    lavThree.playAnimation();
//                                }
//                            });
//                            //取消加载
////                            cancellable.cancel();
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                    }
//                });
                break;
        }
    }
}
